package com.zmcc.clients;

import com.google.gson.Gson;
import com.zmcc.entity.VoiceMeetingResult;
import com.zmcc.entity.VoiceMeetingTemplate;
import com.zmcc.entity.common.CommonRequest;
import com.zmcc.entity.common.CommonResponse;
import com.zmcc.sender.DefaultSyncClient;
import com.zmcc.sender.ISyncClient;
import com.zmcc.service.IVoiceMeetingRequest;
import com.zmcc.service.impl.VoiceMeetingRequestCommercial;
import com.zmcc.utils.WrapperUtils;

public class VoiceMeetingClient {
    private final IVoiceMeetingRequest requestWrapper;

    private final ISyncClient syncClient;

    private final String appkey;

    private final String appsecret;

    private final String mode;

    public VoiceMeetingClient(VoiceMeetingClient.Builder builder) {
        this.appkey = builder.appkey;
        this.appsecret = builder.appsecret;
        this.mode = builder.mode;
        this.syncClient = DefaultSyncClient.getInstance();
        this.requestWrapper = VoiceMeetingRequestCommercial.getInstance();
    }

    public static class Builder {
        private String appkey;
        private String appsecret;
        private String mode;

        public Builder() {

        }

        public VoiceMeetingClient.Builder appkey(String appkey) {
            this.appkey = appkey;
            return this;
        }

        public VoiceMeetingClient.Builder appsecret(String appsecret) {
            this.appsecret = appsecret;
            return this;
        }

        public VoiceMeetingClient.Builder mode(String mode) {
            this.mode = mode;
            return this;
        }

        public VoiceMeetingClient build() {
            return new VoiceMeetingClient(this);
        }
    }

    public VoiceMeetingResult meetingAddSend(VoiceMeetingTemplate template){
        CommonRequest request = this.requestWrapper.getMeetingAddRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey,appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(),VoiceMeetingResult.class);
    }

    public void meetingAddMemberSend(VoiceMeetingTemplate template){
        CommonRequest request = this.requestWrapper.getMeetingAddMemberRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey,appsecret)));
        this.syncClient.fetch(request);
    }

    public void meetingOperateSend(String conf_id,String cmd,String number){
        CommonRequest request = this.requestWrapper.getMeetingOperateRequest(conf_id, cmd, number);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey,appsecret)));
        this.syncClient.fetch(request);
    }

    public VoiceMeetingResult meetingGetDetailSend(String conf_id){
        CommonRequest request = this.requestWrapper.getMeetingGetDetailRequest(conf_id);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey,appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(),VoiceMeetingResult.class);
    }

    public void meetingEndSend(String conf_id){
        CommonRequest request = this.requestWrapper.getMeetingEndRequest(conf_id);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey,appsecret)));
        this.syncClient.fetch(request);
    }
}
